package com.wyyc.common.login.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wyyc.common.login.service.LoginService;
import com.wyyc.common.user.repository.UserRepository;
import com.wyyc.common.user.service.UserService;
import com.wyyc.entity.MessageCode;
import com.wyyc.entity.ResJson;
import com.wyyc.entity.UsersEntity;
import com.wyyc.ext.ExtInterface;
import com.wyyc.finals.Final;
import com.wyyc.util.Util;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Hwei~ on 2017/6/6.
 */
@Service
public class LoginServiceImpl implements LoginService {

    private static List<MessageCode> messageCodeList = new ArrayList<MessageCode>();

    @Resource
    private UserService userService;
    @Resource
    private UserRepository userRepository;

    @Override
    public ResJson sendCode(String phone) {
        ResJson resJson = new ResJson();
        //效验用户名是否存在
        if (userService.accoPhoneToUser(phone)!=null) {
            //用户存在
            //发送并获取验证码
            //String code = ExtInterface.getCode(phone);
            String code = "123456";
            if (code != null) {
                //发送成功
                resJson.setStatus(0);
                resJson.setDesc("短信验证码发送成功!");
                int x = 0;
                for(MessageCode m:messageCodeList){
                    //循环检查该电话是否获取过验证码
                    if (m.getPhone().equals(phone)){
                        //获取过就修改验证码
                        m.setCode(code);
                        x++;
                    }
                }
                if (x==0){
                    //没有获取就是添加保存
                    MessageCode messageCode = new MessageCode();
                    messageCode.setPhone(phone);
                    messageCode.setCode(code);
                    messageCodeList.add(messageCode);
                }
            } else {
                //发送失败
                resJson.setStatus(1);
                resJson.setDesc("短信验证码发送失败!");
            }
            return resJson;
        }
        //用户不存在
        resJson.setStatus(2);
        resJson.setDesc("当前用户并未注册!");
        return resJson;
    }

    @Override
    public ResJson subLogin(String phone, String code) {
        ResJson resJson = new ResJson();
        //判断手机是否被注册
        int x = 0;
        for (MessageCode m:messageCodeList){
            if (m.getPhone().equals(phone)&&m.getCode().equals(code)){
                x++;
            }
        }
        if (x>0){
            UsersEntity usersEntity = userService.accoPhoneToUser(phone);
            if (usersEntity!=null){
                resJson.setStatus(0);
                resJson.setDesc("用户登录成功!");
                resJson.setResult(JSON.toJSONString(usersEntity));
                return resJson;
            }
            resJson.setStatus(1);
            resJson.setDesc("用户不存在!");
            resJson.setResult("");
            return resJson;
        }
        resJson.setStatus(2);
        resJson.setDesc("验证码错误!");
        resJson.setResult("");
        return resJson;
    }

    @Override
    public ResJson autoLogin(int uid) {
        ResJson resJson = new ResJson();
        UsersEntity usersEntity = userRepository.findOne(uid);
        if (usersEntity!=null){
            resJson.setStatus(0);
            resJson.setDesc("用户登录成功!");
            resJson.setResult(JSON.toJSONString(usersEntity));
            return resJson;
        }
        resJson.setStatus(1);
        resJson.setDesc("用户不存在!");
        resJson.setResult("");
        return resJson;
    }
}
